/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : motor.c
  * @brief          : motor interfaces functions 
  * @author         : Yan Yuanbin
  * @date           : 2023/04/27
  * @version        : v1.0
  ******************************************************************************
  * @attention      : to be tested
  ******************************************************************************
  */
/* USER CODE END Header */


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef DEVICE_MOTOR_H
#define DEVICE_MOTOR_H


/* Includes ------------------------------------------------------------------*/
#include "config.h"
#include "stm32f4xx.h"
#include "pid.h"
#include "bsp_can.h"

/* Exported types ------------------------------------------------------------*/

/**
 * @brief typedef enum that contains the usage for DJI Motor.
 */
 
 
 typedef enum
{
  LF=0,
  RF=1,
  LB=2,
  RB=3,
  Chassis_Motor_NUM=4,

}Chassis_Motor_Info;

typedef enum
{

   Yaw=0,
   Gimbal_Motor_NUM=1,


}Gimbal_Motor_Info;



/**
 * @brief typedef enum that contains the Frame Identifier for DJI Motor Device.
 */
typedef enum
{
  DJI_TxFrame_HIGH = 0x1ffU,
  DJI_TxFrame_LOW = 0x200U,
  DJI_RxFrame_MIDDLE = 0x204U,
  DJI_MotorFrameId_NUM,
}DJI_MotorFrameId_e;


/**
 * @brief typedef enum that contains the Error status for Motor Device.
 */
typedef enum
{
  MOTOR_ERROR_NONE = 0x00U,   /*!< no error */
  MOTOR_CAN_OFFLINE = 0x01U,    /*!< CAN transfer failed */
  MOTOR_OVER_TEMPERATURE = 0x02U,   /*!< abnormal motor temperature */
}Motor_Status_e;

/**
 * @brief typedef enum that contains the type of DJI Motor Device.
 */
typedef enum{
    DJI_GM6020,
    DJI_M3508,
    DJI_M2006,
    DJI_MOTOR_TYPE_NUM,
}DJI_Motor_Type_e;

/**
 * @brief typedef structure that contains the information for the Motor Error handler.
 */
typedef struct 
{
  uint16_t ErrorCount;    /*!< Error status judgment count */
  Motor_Status_e Status;   /*!< Error status */
}DJI_Motor_ErrorrHandler_Typedef;

/**
 * @brief  typedef enum that control mode the type of DMMotor Device..
 */
typedef enum
{
  MIT,
	POSITION_VELOCITY,
	VELOCITY,
}DM_Motor_Control_Mode_Type_e;

typedef enum{

  Motor_Enable,
  Motor_Disable,
  Motor_Save_Zero_Position,
  DM_Motor_CMD_Type_Num,

}DM_Motor_CMD_Type_e;

/**
 * @brief typedef structure that contains the information for the Motor CAN Transfer.
 */
typedef struct
{
  uint32_t TxStdId;   /*!< Specifies CAN transfer identifier */
  uint32_t RxStdId;   /*!< Specifies CAN transfer identifier */
  uint8_t FrameIndex;   /* index for motor transmit frame */
}Motor_CANFrameInfo_typedef;

/**
 * @brief typedef structure that contains the General information for the Motor Device.
 */
typedef struct 
{
  bool Initlized;   /*!< init flag */

  int16_t  Current;   /*!< Motor electric current */
  int16_t  Velocity;    /*!< Motor rotate velocity */
  int16_t  Encoder;   /*!< Motor encoder angle */
  int16_t  Last_Encoder;   /*!< previous Motor encoder angle */
  float    Angle;   /*!< Motor angle in degree */
  uint8_t  Temperature;   /*!< Motor Temperature */
}DJI_Motor_GeneralInfo_Typedef;

/**
 * @brief typedef structure that contains the Param_Range for the DM_Motor .
 */
typedef struct 
{

  float  P_MAX;
	float  V_MAX;
	float  T_MAX;

}DM_Motor_Param_Range_Typedef;

/**
 * @brief typedef structure that contains the information for the DJI Motor Device.
 */
typedef struct 
{
  bool Initlized;   /*!< init flag */
  uint8_t  State; 	/*!< Motor ERROR Message */
  uint16_t  P_int;
	uint16_t  V_int;
	uint16_t  T_int;
	float  Position;   /*!< Motor Positon */
  float  Velocity;   /*!< Motor Velocity  */
  float  Torque;  /*!< Motor Torque */
  float  Temperature_MOS;   /*!< Motor Temperature_MOS */
	float  Temperature_Rotor;   /*!< Motor Temperature_Rotor */
  float  Angle;	
}DM_Motor_GeneralInfo_Typedef;

/**
 * @brief typedef structure that contains the information for the Damiao Motor Device.
 */
typedef struct
{
	DJI_Motor_Type_e Type;   /*!< Type of Motor */
  Motor_CANFrameInfo_typedef CANFrame;    /*!< information for the CAN Transfer */
	DJI_Motor_GeneralInfo_Typedef Data;   /*!< information for the Motor Device */
	DJI_Motor_ErrorrHandler_Typedef ERRORHandler;   /*!< information for the Motor Error */
}DJI_Motor_Info_Typedef;

/**
 * @brief typedef structure that contains the information for the DJI Motor Device.
 */
typedef struct
{
  DM_Motor_Control_Mode_Type_e	Control_Mode;
  Motor_CANFrameInfo_typedef CANFrame;    /*!< information for the CAN Transfer */
	DM_Motor_Param_Range_Typedef Param_Range;
	DM_Motor_GeneralInfo_Typedef Data;   /*!< information for the Motor Device */
}DM_Motor_Info_Typedef;

typedef struct
{
  float Position;
	float Velocity;
	float KP;
	float KD;
	float Torque;
	float Angle;
}DM_Motor_Contorl_Info_Typedef;

extern DJI_Motor_Info_Typedef DJI_Yaw_Motor,Chassis_Motor[4];

extern DM_Motor_Info_Typedef DM_Motor[4];

extern DM_Motor_Contorl_Info_Typedef DM_Motor_Contorl_Info[4];
/* Exported functions prototypes ---------------------------------------------*/
/**
  * @brief  Update the DJI motor Information
  */
extern void DJI_Motor_Info_Update(uint32_t *StdId, uint8_t *Rx_Buf,DJI_Motor_Info_Typedef *DJI_Motor);

extern void DM_Motor_Info_Update(uint32_t *StdId,uint8_t *Rx_Buf,DM_Motor_Info_Typedef *DM_Motor);

extern void DM_Motor_Command(CAN_TxFrame_TypeDef *CAN_TxFrame,DM_Motor_Info_Typedef *DM_Motor,uint8_t CMD);

extern void DM_Motor_CAN_TxMessage(CAN_TxFrame_TypeDef *CAN_TxFrame,DM_Motor_Info_Typedef *DM_Motor,float Postion, float Velocity, float KP, float KD, float Torque);


extern DJI_Motor_Info_Typedef Gimbal_Motor[Gimbal_Motor_NUM];

#endif //DEVICE_MOTOR_H
